<template>
  <div>
  <el-form
    :label-position="right"
    label-width="100px"
    :model="formLabelAlign"
    style="max-width: 460px"
  >
    <el-form-item label="Name">
      <el-input v-model="loginname" />
    </el-form-item>
    <el-form-item label="Activity zone">
      <el-input v-model="loginpwd" />
    </el-form-item>
    <el-form-item>
      <el-button type="primary" @click="onLogin">登录</el-button>
      <el-button>注册</el-button>
    </el-form-item>
  </el-form>
  </div>
</template>

<script>
import router from '../router';
export default {
    inject:["global"],
    data(){
        return{
            loginname:"",
            loginpwd:""
        }
    },
    methods:{
        onLogin:function(){
            const axios=this.global.axios;
            axios
            .post("http://localhost:9000/hp/auth/user/login"
                ,`loginName=${this.loginname}&loginPwd=${this.loginpwd}`)
            .then(resp=>{
                let jwttoken=resp.headers.jwt_token;
                sessionStorage.setItem("token",jwttoken);
                let user=resp.data.obj;
                this.$store.commit("setCurUser",user);//记录到了vueX进行共享，并且记录到了sessionStrange
                let returnUrl=sessionStorage.getItem("returnUrl");
                sessionStorage.removeItem("returnUrl");
                router.push(returnUrl?returnUrl:"/");
            })
        }
    }
}
</script>
